app.controller('sys_groupEditController', ['$scope', 'httpWrapService', 'dialogs', '$state', '$stateParams',
                        '$location', '$anchorScroll', 'AlertFactory','validationService',
                function ($scope, httpWrapService, dialogs, $state, $stateParams, $location, $anchorScroll, AlertFactory,validationService) {
	$scope.entity = {};

    $scope.id = $stateParams.id;
    $scope.type = $stateParams.type || 'add';
    $scope.Alert = AlertFactory.newInstance();
    $scope.entity.parentId = $stateParams.parentId ? $stateParams.parentId : null;

    //$scope.region = {};

    $scope.roles = [];
    $scope.tree1 = {};
    $scope.tree2 = {};
	$scope.setting = {
	}

	$scope.$watch('tree1.obj', function(newValue,oldValue){
		if(newValue && newValue.id){
			$scope.entity.areaId = newValue.id;
		}
	});
	$scope.$watch('tree2.obj', function(newValue,oldValue){
		if(newValue && newValue.id){
			$scope.entity.parentId = newValue.id;
		}
	});
	
    httpWrapService.post("system/region/listAll.do", {}).then(function(res){
	    $scope.data1 = res.data.data;
	    $scope.initSelected1();
    });
	httpWrapService.post("system/group/listAll.do", {}).then(function(res){
		$scope.data2 = res.data.data;
		$scope.initSelected2();
	});
    
    $scope.initSelected1 = function(){
    	if(!$scope.entity || !$scope.data1 || !$scope.entity.areaId) return;
    	for(var index in $scope.data1) {
    		var row = $scope.data1[index];
    		if($scope.entity.areaId == row.id) {
    			$scope.tree1.obj = row;
    			break;
    		}
    	}
    }
    $scope.initSelected2 = function(){
    	if(!$scope.entity || !$scope.data2 || !$scope.entity.parentId) return;
    	for(var index in $scope.data2) {
    		var row = $scope.data2[index];
    		if($scope.entity.parentId == row.id) {
    			$scope.tree2.obj = row;
    			break;
    		}
    	}
    }


     if($scope.id) {
    	 httpWrapService.post('system/group/view/' + $scope.id + '.do').then(function (res) {
    		 $scope.entity = res.data.entity;
    		 $scope.type = 'update';
    		 $scope.initSelected1();
    		 $scope.initSelected2();
    		 for(var i = 0; i < res.data.map.roles.length; i++) {
    			 $scope.roles.push(res.data.map.roles[i].id);
    		 }
    	 });
     }

    $scope.unique = function(value){
        return validationService.unique({
            entityName : "common.rbac.po.Group",
            field : "group",
            value : value,
            id : $scope.id
        })
    }
    $scope.uniqueGroup = function(value){
        return validationService.unique({
            entityName : "common.rbac.po.Group",
            field : "name",
            value : value,
            id : $scope.id
        })
    }

    $scope.save = function () {
        var selectRoles = [];
    	for(var i = 0; i < $scope.roles.length; i++) {
            var obj = {
        		roleId : $scope.roles[i],
                groupId : $scope.id
            }
            selectRoles.push(obj);
        }
        httpWrapService.defaultSave( 'system/group/' + $scope.type + '.do',{
            entity : $scope.entity,
            roles : selectRoles
        }, $scope).then(function (res) {
            $state.go('app.system.group.list');
        });

        // 保存成功返回列表页面

    }

    $scope.canle = function () {
        // 后退到列表页面
        $state.go('app.system.group.list');
    }

}]);
